"""
Case Type   : A兼容性-type功能增强
Case Name   : 使用抽象数据类型：创建member成员方法，member procedure和member function
Create At   : 2025/5/22
Owner       : wan005
Description :
    1.创建type；
    2.创建type body;
    3.创建type body;
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.userNode = Node(node='PrimaryDbAmode')
        self.sh_primy = CommonSH()
        self.db_name = self.userNode.db_name
        self.type = 't_028'

    def test_security(self):
        text = '----pre:加载A库gms插件;expect:成功----'
        self.logger.info(text)
        load_cmd = "create extension if not exists gms_output;"
        load_res = self.sh_primy.execut_db_sql(load_cmd, dbname=self.db_name)
        self.logger.info(load_res)
        self.assertIn('CREATE EXTENSION', load_res, '执行失败' + text)

        text = '----step1:创建type;expect:成功----'
        self.logger.info(text)
        sql_cmd1 = f'''
        create or replace type {self.type} as object(
        len integer,
        wth integer,
        hgt integer,
        member function surface return integer,
        member function volume return integer,
        member procedure display (self in out {self.type}),
        member function display1(self in out {self.type})return varchar2);
            '''
        res1 = self.sh_primy.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.logger.info(res1)
        self.assertEqual('CREATE TYPE', res1, '执行失败' + text)

        text = '----step2:创建type body;expect:成功----'
        self.logger.info(text)
        sql_cmd2 = f'''
        CREATE OR REPLACE TYPE BODY {self.type} AS 
        MEMBER FUNCTION volume RETURN INTEGER IS 
        BEGIN 
        RETURN len * wth * hgt;
        -- RETURN SELF.len * SELF.wth * SELF.hgt;
        END; 
        MEMBER FUNCTION surface RETURN INTEGER IS 
        BEGIN -- not necessary to include SELF in following line 
        RETURN 2 * (len * wth + len * hgt + wth * hgt); 
        END; 
        MEMBER PROCEDURE display (SELF IN OUT  {self.type}) IS 
        BEGIN 
        GMS_OUTPUT.PUT_LINE('Length:'||self.len||' -'||'Width:'||wth||'-'
        ||'Height:'||hgt);
        END; 
        MEMBER function display1 (SELF IN OUT  {self.type}) return varchar2 is 
        BEGIN 
        GMS_OUTPUT.PUT_LINE('Length:'||self.len||'-'||'Width:'||wth||'-'||
        'Height:'||hgt);
        return 'display fun'; 
        END; 
        END; 
        '''
        res2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.logger.info(res2)
        self.assertEqual('CREATE TYPE', res2, '执行失败' + text)

        text = '----step3:创建type body;expect:成功----'
        self.logger.info(text)
        sql_cmd3 = f'''
        CREATE OR REPLACE TYPE BODY {self.type} AS
        MEMBER FUNCTION volume RETURN INTEGER IS
        BEGIN
        RETURN SELF.len * SELF.wth * SELF.hgt;
        END;
        MEMBER FUNCTION surface RETURN INTEGER IS
        BEGIN
        RETURN 2 * (len * wth + len * hgt + wth * hgt);
        END;
        MEMBER PROCEDURE display (SELF IN OUT {self.type}) IS
        BEGIN
        GMS_OUTPUT.PUT_LINE('Length: ' || self.len || ' - ' || 'Width: ' || wth
         || ' - ' || 'Height: ' || hgt);
        END;
        MEMBER function display1 (SELF IN OUT {self.type}) return varchar2 is
        BEGIN
        GMS_OUTPUT.PUT_LINE('Length: ' || self.len || ' - ' || 'Width: ' || wth
         || ' - ' || 'Height: ' || hgt);
        return 'display fun';
        END;
        END;
        '''
        res3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.db_name)
        self.logger.info(res3)
        self.assertEqual('CREATE TYPE', res3, '执行失败' + text)

    def tearDown(self):
        text = '----step1:清理环境;expect:成功----'
        clean_cmd = f'drop type {self.type};'
        clean_res = self.sh_primy.execut_db_sql(clean_cmd, dbname=self.db_name)
        self.logger.info(clean_res)
        self.assertEqual('DROP TYPE', clean_res, text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
